import { tajax } from '../js/tajax';

function shops() {
    tajax({
        url: '/customer/shops',
        method: 'get',
        success: function (data) {
            console.log(data);
        }
    });
}

function getShop(id) {
    tajax({
        url: '/customer/shop/' + id,
        method: 'get',
        success: function (data) {

        }
    });
}

export function submitOrder(commodities, receiveInfo) {
    tajax({
        url: '/customer/order/submit',
        method: 'post',
        data: {
            commodities: commodities,
            receiveInfo: receiveInfo
        },
        success: function (data) {
            alert(data.data)
            window.location.href = '/customer/orders'
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function pay(orderId) {
    tajax({
        url: '/customer/order/pay',
        method: 'post',
        data: {
            orderId: orderId
        },
        success: function (data) {
            alert(data.data)
            window.location.href = window.location.href
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function cancel(orderId) {
    tajax({
        url: '/customer/order/cancel',
        method: 'post',
        data: {
            orderId: orderId
        },
        success: function (data) {
            alert(data.data)
            window.location.href = '/customer/orders'
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function orderPos(orderId, result) {
    tajax({
        url: '/customer/order/pos',
        method: 'post',
        data: {
            orderId: orderId
        },
        success: function (data) {
            if (data.code == 0) {
                result(data.data)
            } else {
                alert(data.message)
            }
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function orderGet(orderId, result) {
    tajax({
        url: '/customer/order/get',
        method: 'post',
        data: {
            orderId: orderId
        },
        success: function (data) {
            if (data.code == 0) {
                result(data.data)
            } else {
                result()
            }
        },
        error: function (data) {
            result()
        }
    });
}

export function allOrders(result) {
    tajax({
        url: '/customer/order/all',
        method: 'post',
        success: function (data) {
            result(data.data)
        },
        error: function (data) {
            result([])
        }
    });
}

export function commentOrder(orderId, shopComment, delivererComment, shopStar, deliverStar) {
    tajax({
        url: '/customer/order/comment',
        method: 'post',
        data: {
            orderId: orderId,
            shopComment: shopComment,
            delivererComment: delivererComment,
            shopStar: shopStar,
            deliverStar: deliverStar
        },
        success: function (data) {
            alert(data.data)
            window.location.reload()
        },
        error: function (data) {
            alert(data.message)
        }
    });
}

export function editReceiveInfo(info) {
    tajax({
        url: '/customer/receiveInfo/edit',
        method: 'post',
        data: {
            info: JSON.stringify(info)
        },
        success: function (data) {
            alert(data.data)
        },
        error: function (data) {
            alert(data.message)
        }

    });
}

export function info(result) {
    tajax({
        url: '/customer/info',
        method: 'post',
        success: function (data) {
            result(data.data)
        },
        error: function (data) {

        }
    });
}